<?php
namespace Client\Model;

use Client\Exception\ClientException;

class User
{
   
    public static function createUid(&$user)
    {
        $user['user_uid'] = $user['user_id'].'000ww';
    }
    
    public static function canUseVoucher($voucher,$user_id,$org_id,$payPrice)
    {
        //优惠券不属于改用户
        if ($voucher->user_id != $user_id) {
            throw new ClientException("优惠券不属于改用户");
            return false;
        }
        
        //优惠券已使用
        if ($voucher->has_use != 0) {
            throw new ClientException("优惠券已使用");
            return false;
        }
        
        //优惠券不在使用期限
        if (!(strtotime($voucher->vt_start_time) <= strtotime(date("y-m-d")) 
            && (strtotime($voucher->vt_end_time) >= strtotime(date("y-m-d")))
                )) {
            throw new ClientException("优惠券不在使用期限");
            return false;
        }
        
        //优惠券不满足限额
        if ($voucher->vt_limit_price > $payPrice) {
            throw new ClientException("优惠券不满足限额");
            return false;
        }

        if (empty($voucher->org_id)) {
            return true;
        } else {
            if ($voucher->org_id == $org_id) {
                return true;
            } else {
                throw new ClientException("优惠券商家不属于该商家");
                return false;
            }
        }
    }
}